﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HRMSys.Model;
using HRMSys.DAL;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.Win32;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;

namespace HRMSys.UI.SystemMgr
{
    /// <summary>
    /// EmployeeListUI.xaml 的交互逻辑
    /// </summary>
    public partial class EmployeeListUI : Window
    {
        public EmployeeListUI()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            EmployeeEditUI ui = new EmployeeEditUI();
            ui.IsInsert = true;
            if (ui.ShowDialog() == true) 
            {
                Employee[] employees = this.LoadData();
                gridEmployee.ItemsSource = employees;
            }
        }

        private Employee[] LoadData() 
        {
            return (new EmployeeDAL()).GetAll(true);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Employee[] employees = this.LoadData();
            gridEmployee.ItemsSource = employees;

            this.InitBaseInfo();
        }

        /// <summary>
        /// 初始化一些基础信息
        /// </summary>
        private void InitBaseInfo()
        {
            /*初始化基础信息*/
            IdNameDAL idNameDAL = new IdNameDAL();
            IdName[] idNames = idNameDAL.GetByCategory("学历");
            columnEducationId.ItemsSource = idNames;

            ///*初始化部门信息*/
            DepartmentDAL departDAL = new DepartmentDAL();
            Department[] departments = departDAL.GetAll(true);
            columnDepartmentId.ItemsSource = departments;

            ///*初始化岗位信息*/
            PositionDAL positionDAL = new PositionDAL();
            Position[] positions = positionDAL.GetAll();
            columnPositionId.ItemsSource = positions;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string sql ;
            List<SqlParameter> paramList;
            this.GetSearchCondition(out sql, out paramList);

            Employee[] employees = (new EmployeeDAL()).Search(sql, paramList);
            gridEmployee.ItemsSource = employees;
        }

        /// <summary>
        /// 整理搜索条件
        /// </summary>
        private void GetSearchCondition(out string sql, out List<SqlParameter> paramList)
        {            
            List<string> whereList = new List<string>();
            paramList = new List<SqlParameter>();

            if (chkName.IsChecked == true)
            {
                whereList.Add("Name=@Name");
                paramList.Add(new SqlParameter("@Name", txtName.Text));
            }
            if (chkInData.IsChecked == true)
            {
                whereList.Add("InData>=@InData and EndData<=@EndData");
                paramList.Add(new SqlParameter("@InData", dpStartDate.SelectedDate));
                paramList.Add(new SqlParameter("@InData", dpEndDate.SelectedDate));
            }
            if (chkDepartment.IsChecked == true)
            {
                whereList.Add("DepartmentId=@DepartmentId");
                paramList.Add(new SqlParameter("@DepartmentId", cboDEpartment.SelectedValue));
            }
            if (chkEducation.IsChecked == true)
            {
                whereList.Add("EducationId=@EducationId");
                paramList.Add(new SqlParameter("@EducationId", cboEducation.SelectedValue));
            }
            if (chkBaseSalary.IsChecked == true)
            {
                whereList.Add("BaseSalary>=@StartSalary and BaseSalary<=@EndSalary");
                paramList.Add(new SqlParameter("@BaseSalary", txtStartSalary.Text));
                paramList.Add(new SqlParameter("@EndSalary", txtEndSalary.Text));
            }

            sql = "select * from T_Employee";
            string whereSql = string.Join(" and ", whereList);
            if (whereSql.Length > 0)
            {
                sql = sql + " where " + whereSql;
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel文件|*.xls";
            if (sfd.ShowDialog() != true) 
            {
                return;
            }
            string fileName = sfd.FileName;

            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("员工数据");
            IRow rowHeader = sheet.CreateRow(0);
            rowHeader.CreateCell(0, CellType.STRING).SetCellValue("姓名");
            rowHeader.CreateCell(1, CellType.STRING).SetCellValue("工号");
            rowHeader.CreateCell(2, CellType.STRING).SetCellValue("入职日期");

            Employee[] employees = (Employee[])gridEmployee.ItemsSource;
            for (int i = 0; i < employees.Length; i++)
            {
                Employee employee = employees[i];

                IRow row = sheet.CreateRow(i + 1);
                row.CreateCell(0, CellType.STRING).SetCellValue(employee.Name);
                row.CreateCell(1, CellType.STRING).SetCellValue(employee.Number);

                ICellStyle cellStyle = workbook.CreateCellStyle();
                IDataFormat dataFormat = workbook.CreateDataFormat();
                cellStyle.DataFormat = dataFormat.GetFormat("yyyy年mm月dd日");
                ICell cell = row.CreateCell(2, CellType.NUMERIC);
                cell.CellStyle = cellStyle;
                cell.SetCellValue(employee.InDate);
            }

            using (Stream stream = File.Create(fileName))
            {
                workbook.Write(stream); 
            }
        }
    }
}
